package edu.wtbu.cs.androiddemo.activity.list;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import edu.wtbu.cs.androiddemo.R;

/**
 * 下来列表Demo
 * P101 图5-2
 * P108 图5-4
 */
public class SpinnerSampleActivity extends AppCompatActivity {

    private EditText editName;
    private EditText editPhone;

    private EditText detailEddress;
    private Spinner provinceSpinner;
    private Spinner citySpinner;
    private Spinner countrySpinner;
    private String provinceNameStr;
    private String cityNameStr;

    private String[] province = {"湖北省","江西省","河南省"};

    private String[] partCity ;
    private String[] partCountry;

    private String[] programLang = {"Java","C#","Python","C++","Kotlin"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_spinner_sample);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        //初始化spinner
        Spinner spinner = findViewById(R.id.spinner);
        ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(this,R.layout.simple_list_item,programLang);
        spinner.setAdapter(stringArrayAdapter);

        initView();
        initData();
    }


    private void initView(){
        editName = findViewById(R.id.edit_name);
        editPhone = findViewById(R.id.edit_phone);
        provinceSpinner = findViewById(R.id.provinceSpinner);
        citySpinner = findViewById(R.id.citySpinner);
        countrySpinner = findViewById(R.id.countrySpinner);
        detailEddress = findViewById(R.id.edit_detailAddress);
    }

    private void initData(){
        editName.setText("蔡徐坤");
        editPhone.setText("13888886666");
        detailEddress.setText("练习两年半小区");
        provinceSpinner.setAdapter(new ArrayAdapter<String>(this,R.layout.simple_list_item,province));
        provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                provinceNameStr = province[position];
                switch (position){
                    case 0:
                        partCity = new String[]{"武汉","黄石","鄂州"};
                        break;
                    case 1:
                        partCity = new String[]{"南昌","九江","上饶"};
                        break;
                    case 2:
                        partCity = new String[]{"郑州","南阳","洛阳"};
                        break;
                }
                citySpinner.setAdapter(new ArrayAdapter<String>(SpinnerSampleActivity.this,R.layout.simple_list_item,partCity));
            }

            /**
             *
             * @param parent The AdapterView that now contains no selected item.
             */
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                cityNameStr = partCity[position];
                switch (position){
                    case 0:
                        partCountry = new String[]{"洪山区","武昌区","江夏区","东湖高新区"};
                        break;
                    case 1:
                        partCountry = new String[]{"黄石港区","西塞山区","下陆区","铁山区"};
                        break;
                    case 2:
                        partCountry = new String[]{"鄂城区","华容区","梁子湖区"};
                        break;
                }
                countrySpinner.setAdapter(new ArrayAdapter<String>(SpinnerSampleActivity.this,R.layout.simple_list_item,partCountry));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

}